using AhpilyServer;
using GameServer.Concurrent;
using Protocol.Constant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameServer.Cache.Match
{
    /// <summary>
    /// 匹配的缓存层
    /// </summary>
    public class MatchCache
    {
        /// <summary>
        /// 用户id 与 房间id 的映射
        /// </summary>
        private Dictionary<int, int> uidRoomIdDict = new Dictionary<int, int>();

        /// <summary>
        /// id 与 房间id 的映射
        /// </summary>
        private Dictionary<int, MatchRoom> idModelDict = new Dictionary<int, MatchRoom>();

        /// <summary>
        /// 代表重用的房间的队列
        /// </summary>
        private Queue<MatchRoom> roomQueue = new Queue<MatchRoom>();

        /// <summary>
        /// 房间id
        /// </summary>
        private ConcurrentInt id = new ConcurrentInt(-1);


        /// <summary>
        /// 进入匹配队列, 进入房间
        /// </summary>
        public MatchRoom Enter(int userId, ClientPeer client)
        {
            foreach (MatchRoom mr in idModelDict.Values)
            {
                if(mr.IsFull())
                {
                    continue;
                }
                else
                {
                    mr.Enter(userId, client);
                    uidRoomIdDict.Add(userId, mr.Id);
                    return mr;
                }
            }
            // 开个房间等其他用户
            MatchRoom room = null;
            if(roomQueue.Count > 0)
            {
                room = roomQueue.Dequeue();
            }
            else
            {
                room = new MatchRoom(id.Add_Get());
            }
            room.Enter(userId, client);
            idModelDict.Add(room.Id, room);
            uidRoomIdDict.Add(userId, room.Id);
            return room;
        }

        /// <summary>
        /// 用户离开匹配房间
        /// </summary>
        public MatchRoom Leave(int userId)
        {
            int roomId = uidRoomIdDict[userId];
            MatchRoom room = idModelDict[roomId];
            room.Leave(userId);
            // 进一步的处理
            uidRoomIdDict.Remove(userId);
            if(room.IsEmpty())
            {
                // 房间空了之后放入重用队列,避免多次创建
                idModelDict.Remove(roomId);
                roomQueue.Enqueue(room);
            }
            return room;
        }

        /// <summary>
        /// 用户是否在匹配房间内
        /// </summary>
        public bool IsMatching(int userId)
        {
            return uidRoomIdDict.ContainsKey(userId);
        }

        /// <summary>
        /// 获取玩家所在的房间
        /// </summary>
        public MatchRoom GetRoom(int userId)
        {
            int roomId = uidRoomIdDict[userId];
            MatchRoom room = idModelDict[roomId];
            return room;
        }

        /// <summary>
        /// 摧毁房间
        /// </summary>
        public void Destory(MatchRoom room)
        {
            idModelDict.Remove(room.Id);
            foreach(var userId in room.UIdClientDict.Keys)
            {
                uidRoomIdDict.Remove(userId);
            }
            // 清空数据
            room.UIdClientDict.Clear();
            room.ReadyUIdList.Clear();

            roomQueue.Enqueue(room);
        }
    }
}
